
import subprocess
import redis
import threading
import time
import logging
from util import CABINET_CURRENT_ORDER_STATUS



logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(__name__)



class SoundPlayback:

    def __init__(self) -> None:

        self.r = redis.Redis(host='localhost', port=6379, db=0)

        self.cabinet_current_order_status = self.r.get(CABINET_CURRENT_ORDER_STATUS)
        
        self.open_door_sound_thread = threading.Thread(target=self.open_door_sound)
        self.close_door_sound_thread = threading.Thread(target=self.close_door_sound)
        self.inventory_commpleted_sound_thread = threading.Thread(target=self.inventory_commpleted_sound)

    def open_door_sound(self):

        logger.info("播放开门提示语音")

        self.cabinet_current_order_status = self.r.get(CABINET_CURRENT_ORDER_STATUS)

        if  self.cabinet_current_order_status==None:

            self.cabinet_current_order_status = ""

        else:
            self.cabinet_current_order_status = self.cabinet_current_order_status.decode()


        if self.cabinet_current_order_status != "补货":

            # 播放音频命令
            command = "mpv ./static/voice/open_door.mp3"

        else:

            command = "mpv ./static/voice/start_restocking.mp3"
            

        time.sleep(1)

            # 执行Shell命令
        subprocess.call(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    def close_door_sound(self):

        logger.info("播放关门提示语音")

        self.cabinet_current_order_status = self.r.get(CABINET_CURRENT_ORDER_STATUS)

        if  self.cabinet_current_order_status==None:

            self.cabinet_current_order_status = ""

        else:
            
            self.cabinet_current_order_status = self.cabinet_current_order_status.decode()


        if self.cabinet_current_order_status != "补货":

            # 播放音频命令
            command = "mpv ./static/voice/close_door.mp3"


            # 执行Shell命令
            subprocess.call(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

            logger.info("播放关门提示语音-结束")


    def inventory_commpleted_sound(self):

        logger.info("播放盘点结束提示语音")

        self.cabinet_current_order_status = self.r.get(CABINET_CURRENT_ORDER_STATUS)

        if  self.cabinet_current_order_status==None:
            self.cabinet_current_order_status = ""
        else:
            self.cabinet_current_order_status = self.cabinet_current_order_status.decode()

        if self.cabinet_current_order_status != "补货":

            # 播放音频命令
            command = "mpv ./static/voice/inventory_completed.mp3"

        else:

            command = "mpv ./static/voice/end_restocking.mp3"

            # 执行Shell命令
        subprocess.call(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)


if __name__ == "__main__":

    sp = SoundPlayback()

    sp.inventory_commpleted_sound_thread.start()